package com.ccp.dev.extend.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.SystemConst;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.extend.service.ExtendSysUserService;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.DictionaryService;
import com.ccp.dev.system.service.SysOrgService;
import com.ccp.dev.system.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 扩展的用戶管理控制器类
 *
 * @author 创新中心-王明亮
 */
@Controller
@Action(ownermodel = SysAuditModelType.USER_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT + "/extend/sysUser")
public class ExtendSysUserController extends SysBaseController {
    @Resource
    SysUserService sysUserService;
    @Resource
    ExtendSysUserService extendSysUserService;

    /**
     * 根据租户取得用户表分页列表
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @Action(description = "根据租户查询用户列表", detail = "查询列表<#if success>成功<#else>失败</#if>", exectype = "查询日志")
    @ResponseBody
    @GetMapping("/listByTenTree")
    public ResultData listByTenTree(HttpServletRequest request) {
        try {
            String dataPrivacyLevelStr = request.getParameter("dataPrivacyLevel");
            QueryFilter queryFilter = new QueryFilter(request);
            if (StringUtil.isNotEmpty(dataPrivacyLevelStr)) {
                queryFilter.addFilter("dataPrivacyLevel", Integer.parseInt(dataPrivacyLevelStr));
            }
            List<SysUser> list = this.extendSysUserService.listByTenTree(queryFilter);
            String currentUserId = ContextUtil.getCurrentUserId();
            for (SysUser user : list) {
                if (user.getUserId().equals(currentUserId)) {
                    user.setIsCurrent(SysUser.IS_CURRENT);
                } else {
                    user.setIsCurrent(SysUser.NOT_CURRENT);
                }
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success", queryFilter.getPageBean().getTotalCount(), list);
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }
    @Resource
    SysOrgService sysOrgService;
    @Resource
    DictionaryService dictionaryService;

    @Action(description = "查询当前登录用户信息",
            detail = "查询<#if success>信息成功<#else>信息失败</#if>",
            exectype = "查询日志")
    @ResponseBody
    @RequestMapping("/getUserInfo")
    public ResultData getCurrentUserInfo(){
        try {
            SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
            String privacyLevel = "";
            List<Dictionary> dictionaryList =  dictionaryService.getByNodeKey("rymj");
            List<Dictionary> collectList = dictionaryList.stream().filter(a->a.getItemValue().equals(sysUser.getPrivacyLevel().toString())).collect(Collectors.toList());
            if(!collectList.isEmpty()){
                privacyLevel = collectList.get(0).getItemName();
            }
            Map<String,Object> map = new HashMap<>(3);
            map.put("fullName",sysUser.getFullName());
            map.put("userId",sysUser.getUserId());
            map.put("privacyLevel",privacyLevel);
            map.put("picture",sysUser.getPicture());
            /*
            modify by NiuDong
            组织管理 前端列表树需要顶级id
             */
            if(SystemConst.SUPER_ADMIN_ID.equals(sysUser.getUserId())){
                map.put("orgId",SystemConst.TOP_ORG_SUP_ID);
            }else{
                SysOrg orgTemp = sysOrgService.getOrgByUserId(ContextUtil.getCurrentUserId());
                map.put("orgId",orgTemp.getOrgSupId());
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",0,map);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }


    /**
     * 页面跳转 查看
     */
    @Action(description = "用户详情页面跳转",exectype = "页面跳转日志")
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX+"/view")
    public String viewInfo(HttpServletRequest request , Model model){
        String userId = RequestUtil.getString(request,"userId");
        model.addAttribute("userId",userId);
        model.addAttribute("picture","");
        if(StringUtil.isNotEmpty(userId)){
            SysUser sysUser = this.sysUserService.getOneById(userId);
            model.addAttribute("picture",sysUser.getPicture());
        }
        return "workflow/sysUserView";
    }



}
